<?php

namespace App\Model;


use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class CostStatistics extends BaseModel
{
    protected $connection = 'mysql';
    protected $table = 'cost_statistics';
    public $timestamps = false;
    
    public function getRechargePriceAttribute($value){
        if(!$value) return 0;
        $value = $value / 100;
        return number_format($value);
    }
    
    public function getRechargeDiamondAttribute($value){
        if(!$value) return 0;
        return number_format($value);
    }
    
    public function getGiftDiamondAttribute($value){
        if(!$value) return 0;
        return number_format($value);
    }
    
    public function getGoodDiamondAttribute($value){
        if(!$value) return 0;
        return number_format($value);
    }
    
    public function getExchangeDiamondAttribute($value){
        if(!$value) return 0;
        return number_format($value);
    }
    
    public function getKkValueAttribute($value){
        if(!$value) return 0;
        return number_format($value / 100, 2);
    }
    
    public function getUseKkValueAttribute($value){
        $value = abs($value);
        if(!$value) return 0;
        return number_format($value / 100, 2);
    }
    
    public function getWithdrawKkValueAttribute($value){
        if(!$value) return 0;
        return number_format($value / 100, 2);
    }
    
    public function getList(Request $request){
        $type = $request->input('type','d');
        $limit = $request->input('limit',10);
        $page = $request->input('page',1);
        $start_date = $request->input('start_date');
        $end_date = $request->input('end_date');
    
        if($type === 'd'){
            $query = new self();
    
            if($start_date && $end_date) $query = $query->whereBetween('created_at', [ $start_date, $end_date ]);
            if($start_date && !$end_date) $query= $query->where('created_at','>=', $start_date);
            if(!$start_date && $end_date) $query = $query->where('created_at','<=', $end_date);
            
            return $query->orderBy('id', 'DESC')->paginate($limit,['*'],'page',$page);
        }
        
        $page = ($page-1)*$limit;
        $query = new self();
    
        if($start_date && $end_date) {
            $query = $query->whereBetween("created_at", [ date('Y-m',strtotime($start_date)), date('Y-m',strtotime($end_date)) ]);
        }
        if($start_date && !$end_date) {
            $query= $query->where('created_at','>=', date('Y-m',strtotime($start_date)));
        }
        if(!$start_date && $end_date) {
            $query = $query->where('created_at','<=', date('Y-m',strtotime($end_date)));
        }
        $total = CostStatistics::groupBy('c')->get([DB::raw("DATE_FORMAT(created_at,'%Y-%m') AS c")])->count();
        
        $list = $query->groupBy('c')->orderBy('c','DESC')
            ->limit($limit)
            ->offset($page)
            ->get([
            DB::raw("SUM(recharge_price) AS recharge_price"),
            DB::raw("SUM(recharge_diamond) AS recharge_diamond"),
            DB::raw("SUM(recharge_user) AS recharge_user"),
            DB::raw("SUM(gift_diamond) AS gift_diamond"),
            DB::raw("SUM(good_diamond) AS good_diamond"),
            DB::raw("SUM(exchange_diamond) AS exchange_diamond"),
            DB::raw("SUM(kk_value) AS kk_value"),
            DB::raw("SUM(use_kk_value) AS use_kk_value"),
            DB::raw("SUM(withdraw_kk_value) AS withdraw_kk_value"),
            DB::raw("DATE_FORMAT(created_at,'%Y-%m') AS c"),
            DB::raw("DATE_FORMAT(created_at,'%Y-%m') AS created_at"),
        ]);
        return [ 'data' => $list, 'total'=> $total ];
    }
}
